package schema

import (
	"lianzi_gos/internal/config"
	"time"

	"lianzi_gos/pkg/util"
)

// 品牌表
type Brand struct {
	ID          string    `json:"id" gorm:"size:20;primarykey;"` // Unique ID
	Name        string    `json:"name" gorm:"size:32;index"`     // 品牌名称
	Img         string    `json:"img" gorm:"size:200;index"`     // 品牌图片
	PID         string    `json:"pid" gorm:"size:20;index"`      // 品牌图片
	Description string    `json:"description" gorm:"size:1024"`  // Details about dictionary
	Sequence    int       `json:"sequence" gorm:"index;"`        // Sequence for sorting
	Status      string    `json:"status" gorm:"size:20;index"`   // Status of dictionary (disabled, enabled)
	CreatedAt   time.Time `json:"created_at" gorm:"index;"`      // Create time
	UpdatedAt   time.Time `json:"updated_at" gorm:"index;"`      // Update time
}

func (a *Brand) TableName() string {
	return config.C.FormatTableName("brand")
}

// Defining the query parameters for the `Brand` struct.
type BrandQueryParam struct {
	util.PaginationParam
	LikeName string `form:"name"` // Display name of menu
	Status   string `form:"-"`    // Status of dictionary (disabled, enabled)
}

// Defining the query options for the `Brand` struct.
type BrandQueryOptions struct {
	util.QueryOptions
}

// Defining the query result for the `Brand` struct.
type BrandQueryResult struct {
	Data       Brands
	PageResult *util.PaginationResult
}

// Defining the slice of `Brand` struct.
type Brands []*Brand

func (a Brands) Len() int {
	return len(a)
}

func (a Brands) Less(i, j int) bool {
	if a[i].Sequence == a[j].Sequence {
		return a[i].CreatedAt.Unix() > a[j].CreatedAt.Unix()
	}
	return a[i].Sequence > a[j].Sequence
}

func (a Brands) Swap(i, j int) {
	a[i], a[j] = a[j], a[i]
}

// Defining the data structure for creating a `Brand` struct.
type BrandForm struct {
	Name        string `json:"name" binding:"required,max=32"`                   // 品牌名称
	Description string `json:"description"`                                      // Details about dictionary
	Img         string `json:"img"`                                              // Details about dictionary
	Sequence    int    `json:"sequence"`                                         // Sequence for sorting
	Status      string `json:"status" binding:"required,oneof=disabled enabled"` // Status of dictionary (disabled, enabled)
}

// A validation function for the `BrandForm` struct.
func (a *BrandForm) Validate() error {
	return nil
}

// Convert `BrandForm` to `Brand` object.
func (a *BrandForm) FillTo(brand *Brand) error {
	brand.Name = a.Name
	brand.Description = a.Description
	brand.Img = a.Img
	brand.Sequence = a.Sequence
	brand.Status = a.Status
	return nil
}
